'use strict';

var path = require('path');
var Etcd = require('node-etcd');

// config
var etcd_host = '192.168.102.142';
var etcd_prot = '4001';

var etcd = new Etcd(etcd_host, etcd_prot);

// cb(error,aJson)
function etcdDiscover(name, option, cb) {
	etcd.get(name, option, function(error, value, watcher) {
		if (error) return cb(error);
		cb(null, value, etcd.watcher(name));
	});
}

var name_service_a = '/services/service_a';
etcdDiscover(name_service_a, {
	wait: true
}, function(error, json, watcher) {
	if (error) {
		return console.log(error);
	}

	console.log(name_service_a, json);
	watcher.on('change', function(data) {
		console.log('Value changed; new value: ', json);
	});

	watcher.on('expire', function(data) {
		console.log('Value expired.');
	});

	watcher.on('delete', function(data) {
		console.log('Value deleted.');
	});
});